﻿using UnityEngine;
using System.Collections;

public class ToTarget : MonoBehaviour {
    public Transform Route_1;
    public Transform Route_2;

	// Use this for initialization
	void Start () {

        RouteData(Route_1, true);
        RouteData(Route_2, false);
        StartCoroutine(Move(50));
        StartCoroutine(Move_2(50));

       
	}
	
	// Update is called once per frame
	void Update () {
	
	}
    public float data_1 = 0;
   public float data_2 = 0;
    void RouteData(Transform tran,bool m_bool)
    {
        if (m_bool)
        {
            for (int i = 0; i < tran.GetChild(2).childCount - 1; i++)
            {
                data_1 += Vector2.Distance(tran.GetChild(2).GetChild(i).localPosition, tran.GetChild(2).GetChild(i + 1).localPosition);
            }
        }
        else
        {
            for (int i = 0; i < tran.GetChild(2).childCount - 1; i++)
            {
                data_2 += Vector2.Distance(tran.GetChild(2).GetChild(i).localPosition, tran.GetChild(2).GetChild(i + 1).localPosition);
            }
        }
    }
    IEnumerator Move(float speed)
    {
        bool move = true;
        int index = 0;
        while (move)
        {
            Route_1.GetChild(0).localPosition = Vector2.MoveTowards(Route_1.GetChild(0).localPosition, Route_1.GetChild(2).GetChild(index).localPosition, (Instance<DiMianMove>.instance.Speed / (2100/data_1)) * Time.deltaTime);
            if (Vector2.Distance(Route_1.GetChild(0).localPosition, Route_1.GetChild(2).GetChild(index).localPosition) <= 0.5f)
            {
                index++;
                if (index >= Route_1.GetChild(2).childCount)
                {
                    move = false;
                }
            }
            yield return null;
        }
        
    }

    IEnumerator Move_2(float speed)
    {
        bool move = true;
        int index = 0;
        while (move)
        {
            Route_2.GetChild(0).localPosition = Vector2.MoveTowards(Route_2.GetChild(0).localPosition, Route_2.GetChild(2).GetChild(index).localPosition, (Instance<DiMianMove>.instance.Speed_2/ ( 2100/data_2)) * Time.deltaTime);
            if (Vector2.Distance(Route_2.GetChild(0).localPosition, Route_2.GetChild(2).GetChild(index).localPosition) <= 0.5f)
            {
                index++;
                if (index >= Route_2.GetChild(2).childCount)
                {
                    move = false;
                }
            }
            yield return null;
        }

    }
}
